#!/usr/bin/perl


#@headings = ("Player", "POS", "Team", "G", "AB","R", "H", "2B", "3B", "HR", "RBI", "BB", "K", "SB", "CS", "AVG" ,"SLG", "OBP", "OPS"); 

@headings = ("lahmanid","playerid","managerid","hofid","birthyear","birthmonth","birthday","birthcountry","birthstate","birthcity","deathyear","deathmonth","deathday","deathcountry","deathstate","deathcity","namefirst","namelast","namenote","namegiven","namenick","weight","height","bats","throws","debut","finalgame","college","lahman40id","lahman45id","retroid","holtzid","bbrefid");


$filetoParse = "Master.csv";

$cutoff= "2000";

%players;

$filter = 0;
 
open (FILE, $filetoParse);
while (<FILE>) {
    chomp;

    @line = split(",");


    $count = 0;
    $temp = "{";

    foreach $stat(@line) {

	#save players ID
	if($count == 1)
	{
	    $id = $stat;
	}
	
	#clear new line off input
	if($count == scalar(@headings)-1 )
	{
	    chop($stat);
	}


	
	#Add comma if not the first element
	if($count != 0)                                                                                                                                   
        {                                                                                                                                                 
            $temp = $temp . ",";                                                                                                
        } 


	if ($stat =~ /"/)
	{
	    $temp = $temp . "\"$headings[$count]\":$stat";
	}
	else
	{
	    $temp = $temp . "\"$headings[$count]\":\"$stat\"";
	}


	if($count == 26)
	{
	    ($day,$month,$year) =  split('/',$stat);
	    ($year,$junk) = split(' ',$year);

	    #print "Year: ($year) if blank still playing";
	    
	    if($year < $cutoff && $year != "")
	    {
		$filter = 1;
		#print "filter!\n";
	    }


	}
	

	
	#print "temp-> $temp \n";
	#<>;
	#}
	
	$count++;
	
	
    }

    

    $temp .= "}";

    
    #print "ID-> $id, data->$temp \n";
    #<>;
    
    if($filter == 0)
    {
	$players{$id} = $temp;
    }
    
    $filter = 0;

}
close (FILE);


open (MYFILE, '>>playersCore.js');

print MYFILE "var playersCore = [\n\n";
for $player (keys %players)
{


    #print "$players{$player} \n\n";
    #<>;
    print MYFILE "$players{$player},\n\n\n";;
}
print MYFILE "]; ";
close(MYFILE);
exit;
 
